
package com.market.web.action;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.BeanUtils;

import com.market.bean.Product;
import com.market.bean.Supplier;
import com.market.web.form.ProductForm;
import com.market.web.form.SupplierForm;

public class SupListAction extends SupplierAction
{
    public String execute() throws Exception
    {
        List supList = getSupplierService().getSupplier();
        // List supList =
        // ListUtils.convert(list,Supplier.class,SupplierForm.class);
        List sup = convertSup(supList);
        getRequest().setAttribute("supplier", sup);
        return SUCCESS;
        //return mapping.findForward("suplist");
    }

    private List convertSup(List supList)
    {
        List supfList = new LinkedList();
        for (int i = 0; i < supList.size(); i++)
        {
            Supplier sup = (Supplier)supList.get(i);
            SupplierForm supf = new SupplierForm();
            BeanUtils.copyProperties(sup, supf);
            Iterator ite = getProductService().findBySupplier(sup.getId(), 0, 5).iterator();// sup.getProduct().iterator();
            Set set = new HashSet();
            while (ite.hasNext())
            {
                Product pro = (Product)ite.next();
                ProductForm prof = new ProductForm();
                BeanUtils.copyProperties(pro, prof);
                set.add(prof);
            }
            supf.setProductf(set);
            supfList.add(supf);

        }
        return supfList;
    }

}
